package com.ruoyi.eam.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.eam.doman.EamAsset;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.eam.doman.EamMaintain;
import com.ruoyi.eam.service.IEamMaintainService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 资产维修Controller
 *
 * @author xl
 * @date 2024-07-16
 */
@RestController
@RequestMapping("/maintain")
public class EamMaintainController extends BaseController
{
    @Autowired
    private IEamMaintainService eamMaintainService;
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 查询资产维修列表
     */
    //@RequiresPermissions("eam:maintain:list")
    @GetMapping("/list")
    public TableDataInfo list(EamMaintain eamMaintain)
    {
        startPage();
        List<EamMaintain> list = eamMaintainService.selectEamMaintainList(eamMaintain);
        return getDataTable(list);
    }

    //@RequiresPermissions("eam:maintain:list")
    @GetMapping("/assetMaintainStatus")
    public TableDataInfo assetMaintainStatus(EamAsset eamAsset)
    {
        startPage();
        List<EamAsset> list = eamMaintainService.queryAssetMaintain(eamAsset);
        return getDataTable(list);
    }

    /**
     * 导出资产维修列表
     */
    //@RequiresPermissions("eam:maintain:export")
    @Log(title = "资产维修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamMaintain eamMaintain)
    {
        List<EamMaintain> list = eamMaintainService.selectEamMaintainList(eamMaintain);
        ExcelUtil<EamMaintain> util = new ExcelUtil<EamMaintain>(EamMaintain.class);
        util.exportExcel(response, list, "资产维修数据");
    }

    /**
     * 获取资产维修详细信息
     */
    //@RequiresPermissions("eam:maintain:query")
    @GetMapping(value = "/{maintainId}")
    public AjaxResult getInfo(@PathVariable("maintainId") Long maintainId)
    {
        return success(eamMaintainService.selectEamMaintainByMaintainId(maintainId));
    }

    /**
     * 新增资产维修
     */
    //@RequiresPermissions("eam:maintain:add")
    @Log(title = "资产维修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EamMaintain eamMaintain)
    {
        //锁
        RLock lock = redissonClient.getLock("myinsertMaintainCode");
        try{
            if (!lock.tryLock()) {
                return AjaxResult.error("操作过于频繁");
            }else{
                return toAjax(eamMaintainService.insertEamMaintain(eamMaintain));
            }
        }finally {
            //释放
            lock.unlock();
        }
    }

    /**
     * 修改资产维修
     */
    //@RequiresPermissions("eam:maintain:edit")
    @Log(title = "资产维修", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody EamMaintain eamMaintain)
    {
        return toAjax(eamMaintainService.updateEamMaintain(eamMaintain));
    }

    /**
     * 删除资产维修
     */
    //@RequiresPermissions("eam:maintain:remove")
    @Log(title = "资产维修", businessType = BusinessType.DELETE)
	@DeleteMapping("/{maintainIds}")
    public AjaxResult remove(@PathVariable Long[] maintainIds)
    {
        return toAjax(eamMaintainService.deleteEamMaintainByMaintainIds(maintainIds));
    }

    @Log(title = "修改:资产", businessType = BusinessType.UPDATE)
    @PutMapping("/updateMaintainsStatus")
    public AjaxResult updateMaintainsStatus(@RequestBody List<EamMaintain> maintains)
    {

        int num = 0;
        for (EamMaintain eamMaintain : maintains) {
            num = eamMaintainService.updateEamMaintain(eamMaintain);
        }
        return toAjax(num);
    }
}
